//
//  PingLunOfGuanFangActivityViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/5/31.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "PingLunOfGuanFangActivityViewController.h"
#import "PingLunOnZiXunTableViewCell.h"
#import "SendPingLunViewController.h"

@interface PingLunOfGuanFangActivityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *pingLunOfGuanFangActivityTableV;

@property (nonatomic, strong) UIImageView *dianZanRenShuImageV; // 点赞人数的图片

@property (nonatomic, strong) UILabel *dianZanRenShuLabel; // 点赞人数

@end

@implementation PingLunOfGuanFangActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"评论";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 布局
- (void)addMySubview
{
    self.pingLunOfGuanFangActivityTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 80 * kAdjustParamater) style:UITableViewStyleGrouped];
    self.pingLunOfGuanFangActivityTableV.backgroundColor = [UIColor whiteColor];
    self.pingLunOfGuanFangActivityTableV.dataSource = self;
    self.pingLunOfGuanFangActivityTableV.delegate = self;
    self.pingLunOfGuanFangActivityTableV.bounces = NO;
    [self.view addSubview:_pingLunOfGuanFangActivityTableV];
    
    // 添加下方的发表评论
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 64 - 60 * kAdjustParamater, kScreenWidth, 61 * kAdjustParamater)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    // 灰色条
    UILabel *grayLineOnFooter = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 2 * kAdjustParamater)];
    grayLineOnFooter.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [bottomView addSubview:grayLineOnFooter];
    
    // 发表评论背景view
    UIView *pingLunView = [[UIView alloc] initWithFrame:CGRectMake(37 * kAdjustParamater, 13 * kAdjustParamater, 115 * kAdjustParamater, 31 * kAdjustParamater)];
    pingLunView.backgroundColor = [UIColor whiteColor];
    pingLunView.userInteractionEnabled = YES;
    pingLunView.layer.cornerRadius = 7 * kAdjustParamater;
    pingLunView.layer.borderColor = [kCyanColor CGColor];
    pingLunView.layer.borderWidth = 1 * kAdjustParamater;
    [bottomView addSubview:pingLunView];
    UITapGestureRecognizer *pingLunTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pingLunGes:)];
    [pingLunView addGestureRecognizer:pingLunTapGes];
    
    // 发表评论的图片
    UIImageView *faBiaoPingLunImageV = [[UIImageView alloc] initWithFrame:CGRectMake(4 * kAdjustParamater, 5 * kAdjustParamater, 21 * kAdjustParamater, 21 * kAdjustParamater)];
    faBiaoPingLunImageV.image = [UIImage imageNamed:@"xiepinglunonzixun"];
    [pingLunView addSubview:faBiaoPingLunImageV];
    
    // 发表评论标题
    UILabel *faBiaoPingLunLabel = [[UILabel alloc] initWithFrame:CGRectMake(faBiaoPingLunImageV.right + 4 * kAdjustParamater, 5 * kAdjustParamater, 75 * kAdjustParamater, 21 * kAdjustParamater)];
    // faBiaoPingLunLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    faBiaoPingLunLabel.text = @"发表评论";
    faBiaoPingLunLabel.textColor = kCyanColor;
    faBiaoPingLunLabel.textAlignment = NSTextAlignmentCenter;
    faBiaoPingLunLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k18Big];
    [pingLunView addSubview:faBiaoPingLunLabel];
    
    // 点赞人数背景
    UIView *dianZanRenShuView = [[UIView alloc] initWithFrame:CGRectMake((375 - 152) * kAdjustParamater, 13 * kAdjustParamater, 115 * kAdjustParamater, 31 * kAdjustParamater)];
    dianZanRenShuView.backgroundColor = [UIColor whiteColor];
    dianZanRenShuView.userInteractionEnabled = YES;
    dianZanRenShuView.layer.cornerRadius = 7 * kAdjustParamater;
    dianZanRenShuView.layer.borderColor = [kYellowColor CGColor];
    dianZanRenShuView.layer.borderWidth = 1 * kAdjustParamater;
    [bottomView addSubview:dianZanRenShuView];
    UITapGestureRecognizer *dianZanRenShuTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dianZanRenGes:)];
    [dianZanRenShuView addGestureRecognizer:dianZanRenShuTapGes];
    // 点赞人数图片
    self.dianZanRenShuImageV = [[UIImageView alloc] initWithFrame:CGRectMake(4 * kAdjustParamater, 5 * kAdjustParamater, 21 * kAdjustParamater, 21 * kAdjustParamater)];
    
    [dianZanRenShuView addSubview:_dianZanRenShuImageV];
    
    // 点赞人数标题
    self.dianZanRenShuLabel = [[UILabel alloc] initWithFrame:CGRectMake(faBiaoPingLunImageV.right + 4 * kAdjustParamater, 5 * kAdjustParamater, 75 * kAdjustParamater, 21 * kAdjustParamater)];
   //  self.dianZanRenShuLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
 //   self.dianZanRenShuLabel.text = [NSString stringWithFormat:@"%@人点赞", [self.ziXunContentDic valueForKey:@"like_num"]];
    self.dianZanRenShuLabel.text = @"333人点赞";
    self.dianZanRenShuLabel.textColor = kCyanColor;
    self.dianZanRenShuLabel.textAlignment = NSTextAlignmentCenter;
    self.dianZanRenShuLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k18Big];
    [dianZanRenShuView addSubview:_dianZanRenShuLabel];
    
    // 自适应宽度
    CGRect newFrameOfDianZanRenShu = self.dianZanRenShuLabel.frame;
    newFrameOfDianZanRenShu.size.width = [[NetStatusHandle shareHandle] adjustWidth:self.dianZanRenShuLabel.text size:18 height:21];
    self.dianZanRenShuLabel.frame = newFrameOfDianZanRenShu;
    dianZanRenShuView.width = self.dianZanRenShuLabel.width + 29 * kAdjustParamater + 9 * kAdjustParamater;
}

#pragma mark - UITableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"guanFangHuoDongCell";
    PingLunOnZiXunTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[PingLunOnZiXunTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    UITapGestureRecognizer *dianZanTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dianZanGes:)];
    [cell.dianZanImageV addGestureRecognizer:dianZanTapGes];
    UITapGestureRecognizer *deletePingLunTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deletePingLunGes:)];
    [cell.laJiTongImageV addGestureRecognizer:deletePingLunTapGes];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 动态返回高度
    PingLunOnZiXunTableViewCell *pingLunCell = (PingLunOnZiXunTableViewCell *)[self tableView:self.pingLunOfGuanFangActivityTableV cellForRowAtIndexPath:indexPath];
    return pingLunCell.grayBackGroundView.bottom + 12 * kAdjustParamater;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 39 * kAdjustParamater)];
    headView.backgroundColor = [UIColor whiteColor];
    // 评论label
    UILabel *pingLunLabel = [[UILabel alloc] initWithFrame:CGRectMake(25 * kAdjustParamater, 12 * kAdjustParamater, 100 * kAdjustParamater, 14 * kAdjustParamater)];
    //  pingLunLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    
    pingLunLabel.text = [NSString stringWithFormat:@"评论10"];
    pingLunLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    pingLunLabel.textAlignment = NSTextAlignmentLeft;
    pingLunLabel.font = [UIFont systemFontOfSize:k14Big];
    [headView addSubview:pingLunLabel];
    
    // 点赞人数
    UILabel *dianZanLabel = [[UILabel alloc] initWithFrame:CGRectMake((375 - 25 - 100) * kAdjustParamater, 12 * kAdjustParamater, 100 * kAdjustParamater, 14 * kAdjustParamater)];
    // dianZanLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
//    self.dianZanCount = 0;
//    self.allPingLunDataArray = [NSMutableArray arrayWithArray:self.pingLunArray];
//    for (int i = 0; i < self.allPingLunDataArray.count; i++) {
//        ZiXunGeRenPingLunModel *model = self.allPingLunDataArray[i];
//        // 点赞人数
//        NSInteger likeNum = [model.like_num integerValue];
//        self.dianZanCount = self.dianZanCount + likeNum;
//    }
 //   dianZanLabel.text = [NSString stringWithFormat:@"赞%ld", self.dianZanCount];
    dianZanLabel.text = [NSString stringWithFormat:@"赞100"];
    dianZanLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    dianZanLabel.textAlignment = NSTextAlignmentRight;
    dianZanLabel.font = [UIFont systemFontOfSize:k14Big];
    [headView addSubview:dianZanLabel];
    
    // 底部灰色条
    UILabel *grayLineOnBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, 38 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
    grayLineOnBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.08];
    [headView addSubview:grayLineOnBottom];
    return headView;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 39 * kAdjustParamater;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
    footerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20 * kAdjustParamater;
}

#pragma mark - 点赞
- (void)dianZanGes:(UITapGestureRecognizer *)tapGes
{
    UIView *contentV = [tapGes.view superview];
    PingLunOnZiXunTableViewCell *cell = (PingLunOnZiXunTableViewCell *)[contentV superview];
    NSIndexPath *tempIndexPath = [self.pingLunOfGuanFangActivityTableV indexPathForCell:cell];
    NSLog(@"对%ld进行点赞", tempIndexPath.row);

}

#pragma mark - 垃圾桶
- (void)deletePingLunGes:(UITapGestureRecognizer *)tapGes
{
    UIView *contentV = [tapGes.view superview];
    PingLunOnZiXunTableViewCell *cell = (PingLunOnZiXunTableViewCell *)[contentV superview];
    NSIndexPath *tempIndexPath = [self.pingLunOfGuanFangActivityTableV indexPathForCell:cell];
    NSLog(@"对%ld进行删除", tempIndexPath.row);
    [self addAlertOnGuanFangActivity:@"您确定要删除该评论么" IndexPath:tempIndexPath];
}

#pragma mark - 提醒框
- (void)addAlertOnGuanFangActivity:(NSString *)content IndexPath:(NSIndexPath *)indexPath
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:content preferredStyle:UIAlertControllerStyleAlert];
    // 修改标题颜色
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"警告"];
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertVC setValue:title forKey:@"attributedTitle"];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //   NSLog(@"确定");
        
        
        
    }];
    [alertVC addAction:sureAction];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark - 发表评论
- (void)pingLunGes:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"发表评论");
    SendPingLunViewController *sendPingLunVC = [[SendPingLunViewController alloc] init];
    sendPingLunVC.isZiXunOrGuanFangActivity = @"guanFangActivity";
    [self.navigationController pushViewController:sendPingLunVC animated:YES];
}

#pragma mark - 对活动进行总人数点赞
- (void)dianZanRenGes:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"对活动进行点赞的总人数");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
